package com.zyy.ui.exception;

import com.zyy.ui.dto.ResultDTO;
import com.zyy.ui.utils.LogUtil;
import com.zyy.ui.utils.ResultUtil;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.servlet.http.HttpServletRequest;
import javax.validation.ConstraintViolation;
import javax.validation.ConstraintViolationException;
import java.util.Set;

/**
 * @ClassName: GlobalExceptionHandler
 * @Description: 统一异常处理
 * @Author: zyy
 * @Date: 2020/05/02 19:07
 * @Version: 1.0
 */
@RestControllerAdvice
public class GlobalExceptionHandler<T> {

    @ExceptionHandler(MethodArgumentNotValidException.class)
    public ResultDTO<T> handleMethodArgumentNotValidException(MethodArgumentNotValidException ex, HttpServletRequest request) {
        BindingResult bindingResult = ex.getBindingResult();
        StringBuilder stringBuilder = new StringBuilder();
        bindingResult.getAllErrors().forEach(objectError -> {
            stringBuilder.append(objectError.getDefaultMessage() + "~");
        });
        ResultDTO resultDTO = ResultUtil.getArgsValidFailResult(stringBuilder.toString());
        resultDTO.setPath(request.getRequestURI());
        LogUtil.info("参数校验失败", resultDTO);
        return resultDTO;
    }


    @ExceptionHandler(ConstraintViolationException.class)
    public ResultDTO<T> handleConstraintViolationException(ConstraintViolationException ex, HttpServletRequest request) {
        Set<ConstraintViolation<?>> constraintViolationSet = ex.getConstraintViolations();
        StringBuilder stringBuilder = new StringBuilder();
        constraintViolationSet.forEach(constraintViolation -> {
            stringBuilder.append(constraintViolation.getMessage() + "~");
        });
        ResultDTO resultDTO = ResultUtil.getArgsValidFailResult(stringBuilder.toString());
        resultDTO.setPath(request.getRequestURI());
        LogUtil.info("参数校验失败", resultDTO);
        return resultDTO;
    }


    @ExceptionHandler(DaoException.class)
    public ResultDTO<T> handleDaoException(DaoException de, HttpServletRequest request) {
        ResultDTO resultDTO = ResultUtil.getFailResult(de);
        resultDTO.setPath(request.getRequestURI());
        return resultDTO;
    }

    @ExceptionHandler(DealException.class)
    public ResultDTO<T> handleDealException(DealException de, HttpServletRequest request) {
        ResultDTO resultDTO = ResultUtil.getFailResult(de);
        resultDTO.setPath(request.getRequestURI());
        return resultDTO;
    }

    @ExceptionHandler(SaoException.class)
    public ResultDTO<T> handleSaoException(SaoException de, HttpServletRequest request) {
        ResultDTO resultDTO = ResultUtil.getFailResult(de);
        resultDTO.setPath(request.getRequestURI());
        return resultDTO;
    }

    @ExceptionHandler(Exception.class)
    public ResultDTO<T> handleException(Exception e, HttpServletRequest request) {
        LogUtil.error(e, "程序异常~");
        ResultDTO resultDTO = ResultUtil.getFailResult();
        resultDTO.setPath(request.getRequestURI());
        return resultDTO;
    }
}
